#!/usr/bin/env python
""" Forms a 'text entry window' that takes a text up to 8 chars """
""" wide and prints it. """
import pygtk
pygtk.require('2.0')
import gtk,sys,os

global adjusted
adjusted=False
class GUI:
	def keypress(self, widget, event) :
		if event.keyval == gtk.keysyms.Escape :
			gtk.main_quit()
		elif event.keyval == gtk.keysyms.Left :
			global builder
			global adjusted
			v=builder.get_object("hscale1").get_adjustment().value
			builder.get_object("hscale1").get_adjustment().value=max(v-0.1,0.1)
			adjusted=True
		elif event.keyval == gtk.keysyms.Right :
			v=builder.get_object("hscale1").get_adjustment().value
			builder.get_object("hscale1").get_adjustment().value=min(v+0.1,0.9)
			adjusted=True
		elif event.keyval == gtk.keysyms.Return:
			if adjusted:
				proportion=builder.get_object("hscale1").get_adjustment().value
				return self.cmd('adjustProportion '+str(proportion))
	def adjustproportion(self, widget):
		global proportion
		proportion=widget.get_adjustment().value

	def apply(self,widget):
		global proportion
		return self.cmd('adjustProportion '+str(proportion))

	def unfocused(self,widget,event):
		exit(True)
		return False
		
	def delete_event(self, widget, event, data=None):
		# If you return FALSE in the "delete_event" signal handler,
		# GTK will emit the "destroy" signal. Returning TRUE means
		# you don't want the window to be destroyed.
		# This is useful for popping up 'are you sure you want to quit?'
		# type dialogs.
		print "delete event occurred"

		# Change FALSE to TRUE and the main window will not be destroyed
		# with a "delete_event".
		return False
	def goto(self,w):
		print('w '+w)
		exit(True)
		return False
	def shift(self,w):
		print('shift '+w)
		exit(True)
		return False
	def cmd(self,w):
		print(w)
		exit(True)
		return False
	
	""" Form the GUI and bootstrap the application """
	def goto1(self, widget):
		return self.goto('1')
	def goto2(self, widget):
		return self.goto('2')
	def goto3(self, widget):
		return self.goto('3')
	def goto4(self, widget):
		return self.goto('4')
	def shift1(self, widget):
		return self.shift('1')
	def shift2(self, widget):
		return self.shift('2')
	def shift3(self, widget):
		return self.shift('3')
	def shift4(self, widget):
		return self.shift('4')
	def layout_v(self,widget): return self.cmd('changeMode v')
	def layout_h(self,widget): return self.cmd('changeMode h')
	def layout_f(self,widget): return self.cmd('changeMode f')
	def layout_l(self,widget): return self.cmd('changeMode l')
	def layout_t(self,widget): return self.cmd('changeMode t')
	def console(self,widget): return self.cmd('launch-gui-app gnome-terminal')
	def invertProportion(self,widget): return self.cmd('invertProportion')
	def moveUp(self,widget): return self.cmd('moveUp')
	def moveDown(self,widget): return self.cmd('moveDown')
	def sizeUp(self,widget): return self.cmd('+')
	def sizeDown(self,widget): return self.cmd('-')
	def sizeUp2(self,widget): return self.cmd('v+')
	def sizeDown2(self,widget): return self.cmd('v-')
	def retile(self,widget): return self.cmd('tile')
	def toggleFullscreen(self,widget): return self.cmd('F')
	def close_active(self,widget): return self.cmd('c')
	def rotateMonitor(self,widget): return self.cmd('rotate')

		
	def __init__(self):
		global builder
		builder=gtk.Builder()
		HOME = os.environ.get("HOME")
		builder.add_from_file(os.path.join(HOME, ".config/mtiler/mtiler_ui2.glade"))
		self.window=builder.get_object("window1")
		self.window.set_decorated(False)
		self.window.set_app_paintable(True)
		self.window.set_position(gtk.WIN_POS_CENTER)
		self.window.set_skip_taskbar_hint(True)
		self.window.set_keep_above(True)
		self.window.connect("key-press-event", self.keypress)
		self.window.connect("focus-out-event", self.unfocused)
		self.window.stick()
		builder.connect_signals(self)
		
		global proportion
		proportion=sys.argv[2]
		self.scale=builder.get_object("hscale1")
		self.scale.set_adjustment(gtk.Adjustment(lower=0.1, upper=0.9, value=float(proportion)))
		self.scale.connect("value-changed", self.adjustproportion)
		mode=sys.argv[1]
		# This sets the self.windowdows colormap, so it supports transparency.
		# This will only work if the wm support alpha channel
		# screen = self.window.get_screen()
		# rgba = screen.get_rgba_colormap()
		# self.window.set_colormap(rgba)
		# self.window.set_opacity(0.5)

	def input_cb(self, pwidget):
		print (self.winput.get_text())
		gtk.main_quit()

if __name__ == '__main__':
	if len(sys.argv)>0:
		myGUI = GUI()
		myGUI.window.show()
		gtk.main()
